package com.gopay.solution.dao.impl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.solution.dao.SsPlSolutionRelDAO;
import com.gopay.solution.domain.po.SsPlSolutionRel;

/**
 * 产品线方案关系映射DAO实现
 * 
 * @SsPlSolutionRelDAOImpl.java
 * @author MA HUAN
 * @2013-3-26 上午11:13:54 www.gopay.com.cn Inc.All rights reserved.
 */

@SuppressWarnings({ "unchecked", "rawtypes" })
@Repository(value = "ssPlSolutionRelDAO")
public class SsPlSolutionRelDAOImpl extends BaseDAOImpl<SsPlSolutionRel, Serializable> implements SsPlSolutionRelDAO {

    @Override
    public SsPlSolutionRel getByPlIdAndSoluType(String prodLineId, String solutionType) {
        
        if(StringUtils.isEmpty(prodLineId) || StringUtils.isEmpty(solutionType)) {
            return null;
        }
        
        String hql = " from SsPlSolutionRel rel where rel.prodLineId = :ProdLineId and rel.solutionType = :SolutionType";

        Map param = new HashMap();
        param.put("ProdLineId", prodLineId);
        param.put("SolutionType", solutionType);

        List<SsPlSolutionRel> res = super.findByHql(hql, param);

        if (res != null && res.size() > 0) {
            if (res.size() == 1)
                return res.get(0);

            // (ProdLineId, solutionType) should be the Pk of this table, Otherwise
            // throw Exception.
            throw new IllegalStateException("More than One Record Showed Up!, ProdLineId =" + prodLineId + " , solutionType = "
                    + solutionType);
        }

        return null;
    }

    @Override
    public List<SsPlSolutionRel> listBySoluType(String solutionType) {
        
        if(StringUtils.isEmpty(solutionType)) {
            return null;
        }
        
        String hql = " from SsPlSolutionRel rel where rel.solutionType = :SolutionType";

        Map param = new HashMap();
        param.put("SolutionType", solutionType);
        
        List<SsPlSolutionRel> res = super.findByHql(hql, param);
        
        return res;
    }

    @Override
    public List<SsPlSolutionRel> listByPlIdSoluType(final List<String> plIds, final String solutionType) {
        
        if(plIds == null || plIds.isEmpty() || StringUtils.isEmpty(solutionType)) {
            return null;
        }
        
        final String hql = " from SsPlSolutionRel rel where rel.prodLineId in (:PlIds) and rel.solutionType = :SolutionType";
        
        Map params = new HashMap();
        params.put("SolutionType", solutionType);
        params.put("PlIds", plIds);
        
        return (List<SsPlSolutionRel>) super.findByHql(hql, params);
    }

}
